//
//  UIColor+Category.m
//  XLDoctor
//
//  Created by longma on 2018/9/4.
//  Copyright © 2018年 XLH. All rights reserved.
//

#import "UIColor+MJUI.h"

@implementation UIColor (MJUI)
+ (UIColor *)mj_randomColor {
    CGFloat red = ( arc4random() % 255 / 255.0 );
    CGFloat green = ( arc4random() % 255 / 255.0 );
    CGFloat blue = ( arc4random() % 255 / 255.0 );
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
+ (UIColor *)mj_colorWithHexString:(NSString *)color alpha:(CGFloat)alpha
{
    return [[UIColor mj_colorWithHexString:color] mj_colorWithAlpha:alpha];
}

- (UIColor *)mj_colorWithAlpha:(CGFloat)customAlpha{
    CGFloat red = 0.0;
    CGFloat green = 0.0;
    CGFloat blue = 0.0;
    CGFloat alpha = 0.0;
    [self getRed:&red green:&green blue:&blue alpha:&alpha];
    UIColor *newColor = [UIColor colorWithRed:red green:green blue:blue alpha:customAlpha];
    return newColor;
}

+(UIColor *)mj_colorWithHexString:(NSString *)hexColor{
    NSString * cString = [[hexColor stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor blackColor];
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    
    if ([cString length] != 6) return [UIColor blackColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString * rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString * gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString * bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float)r / 255.0f)
                           green:((float)g / 255.0f)
                            blue:((float)b / 255.0f)
                           alpha:1];
}

- (NSString *)mj_hexString {
    NSInteger alpha = self.qmui_alpha * 255;
    NSInteger red = self.qmui_red * 255;
    NSInteger green = self.qmui_green * 255;
    NSInteger blue = self.qmui_blue * 255;
    return [[NSString stringWithFormat:@"#%@%@%@%@",
            [self alignColorHexStringLength:[NSString qmui_hexStringWithInteger:alpha]],
            [self alignColorHexStringLength:[NSString qmui_hexStringWithInteger:red]],
            [self alignColorHexStringLength:[NSString qmui_hexStringWithInteger:green]],
            [self alignColorHexStringLength:[NSString qmui_hexStringWithInteger:blue]]] lowercaseString];
}
// 对于色值只有单位数的，在前面补一个0，例如“F”会补齐为“0F”
- (NSString *)alignColorHexStringLength:(NSString *)hexString {
    return hexString.length < 2 ? [@"0" stringByAppendingString:hexString] : hexString;
}

- (CGFloat)mj_red {
    CGFloat r;
    if ([self getRed:&r green:0 blue:0 alpha:0]) {
        return r;
    }
    return 0;
}

- (CGFloat)mj_green {
    CGFloat g;
    if ([self getRed:0 green:&g blue:0 alpha:0]) {
        return g;
    }
    return 0;
}

- (CGFloat)mj_blue {
    CGFloat b;
    if ([self getRed:0 green:0 blue:&b alpha:0]) {
        return b;
    }
    return 0;
}

- (CGFloat)mj_alpha {
    CGFloat a;
    if ([self getRed:0 green:0 blue:0 alpha:&a]) {
        return a;
    }
    return 0;
}

- (NSString *)mj_RGBAString {
    return [NSString stringWithFormat:@"%.0f,%.0f,%.0f,%.2f",
            round(self.mj_red * 255),
            round(self.mj_green * 255),
            round(self.mj_blue * 255),
            self.mj_alpha];
}

- (NSString *)mj_RGBString {
    return [NSString stringWithFormat:@"%.0f,%.0f,%.0f",
            round(self.mj_red * 255),
            round(self.mj_green * 255),
            round(self.qmui_blue * 255)];
}

+ (UIColor *)mj_colorWithHex:(long)hexColor alpha:(float)opacity
{
    
    float red = ((float)((hexColor & 0xFF0000) >> 16))/255.0;
    float green = ((float)((hexColor & 0xFF00) >> 8))/255.0;
    float blue = ((float)(hexColor & 0xFF))/255.0;
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:opacity];
}
@end
